﻿using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using HslCommunication.LogNet;

namespace HslCommunication.Profinet.Knx
{
	/// <summary>
	/// Knx驱动，具体的用法参照demo
	/// </summary>
	/// <remarks>
	/// 感谢上海NULL提供的技术支持
	/// </remarks>
	// Token: 0x02000088 RID: 136
	public class KnxUdp
	{
		/// <summary>
		/// 实例化一个默认的对象
		/// </summary>
		// Token: 0x06000AA1 RID: 2721 RVA: 0x0004002D File Offset: 0x0003E22D
		public KnxUdp()
		{
			this.KNX_CODE = new KnxCode();
		}

		/// <summary>
		/// 通道号（由设备发来）
		/// </summary>
		// Token: 0x170001B9 RID: 441
		// (get) Token: 0x06000AA2 RID: 2722 RVA: 0x00040042 File Offset: 0x0003E242
		// (set) Token: 0x06000AA3 RID: 2723 RVA: 0x0004004A File Offset: 0x0003E24A
		public byte Channel { get; set; }

		/// <summary>
		/// 远程ip地址
		/// </summary>
		// Token: 0x170001BA RID: 442
		// (get) Token: 0x06000AA4 RID: 2724 RVA: 0x00040053 File Offset: 0x0003E253
		// (set) Token: 0x06000AA5 RID: 2725 RVA: 0x0004005B File Offset: 0x0003E25B
		public IPEndPoint RouEndpoint
		{
			get
			{
				return this._rouEndpoint;
			}
			set
			{
				this._rouEndpoint = value;
			}
		}

		/// <summary>
		/// 本机IP地址
		/// </summary>
		// Token: 0x170001BB RID: 443
		// (get) Token: 0x06000AA6 RID: 2726 RVA: 0x00040064 File Offset: 0x0003E264
		// (set) Token: 0x06000AA7 RID: 2727 RVA: 0x0004006C File Offset: 0x0003E26C
		public IPEndPoint LocalEndpoint
		{
			get
			{
				return this._localEndpoint;
			}
			set
			{
				this._localEndpoint = value;
			}
		}

		/// <summary>
		/// 系统的日志信息
		/// </summary>
		// Token: 0x170001BC RID: 444
		// (get) Token: 0x06000AA8 RID: 2728 RVA: 0x00040075 File Offset: 0x0003E275
		// (set) Token: 0x06000AA9 RID: 2729 RVA: 0x0004007D File Offset: 0x0003E27D
		public ILogNet LogNet
		{
			get
			{
				return this.logNet;
			}
			set
			{
				this.logNet = value;
			}
		}

		/// <summary>
		/// 当前的状态是否连接中
		/// </summary>
		// Token: 0x170001BD RID: 445
		// (get) Token: 0x06000AAA RID: 2730 RVA: 0x00040086 File Offset: 0x0003E286
		public bool IsConnect
		{
			get
			{
				return this.KNX_CODE.IsConnect;
			}
		}

		/// <summary>
		/// 通信指令类
		/// </summary>
		// Token: 0x170001BE RID: 446
		// (get) Token: 0x06000AAB RID: 2731 RVA: 0x00040093 File Offset: 0x0003E293
		public KnxCode KnxCode
		{
			get
			{
				return this.KNX_CODE;
			}
		}

		/// <summary>
		/// 和KNX网络进行握手并开始监听
		/// </summary>
		// Token: 0x06000AAC RID: 2732 RVA: 0x0004009C File Offset: 0x0003E29C
		public void ConnectKnx()
		{
			bool flag = this.udpClient == null;
			if (flag)
			{
				this.udpClient = new UdpClient(this.LocalEndpoint)
				{
					Client = 
					{
						DontFragment = true,
						SendBufferSize = 0,
						ReceiveTimeout = 120000
					}
				};
			}
			int num = this.udpClient.Send(this.KNX_CODE.Handshake(this.LocalEndpoint), 26, this.RouEndpoint);
			this.udpClient.BeginReceive(new AsyncCallback(this.ReceiveCallback), null);
			Thread.Sleep(1000);
			bool isConnect = this.KNX_CODE.IsConnect;
			if (isConnect)
			{
				this.KNX_CODE.Return_data_msg += this.KNX_CODE_Return_data_msg;
				this.KNX_CODE.GetData_msg += this.KNX_CODE_GetData_msg;
				this.KNX_CODE.Set_knx_data += this.KNX_CODE_Set_knx_data;
				this.KNX_CODE.knx_server_is_real(this.LocalEndpoint);
			}
		}

		/// <summary>
		/// 保持KNX连接
		/// </summary>
		// Token: 0x06000AAD RID: 2733 RVA: 0x000401A9 File Offset: 0x0003E3A9
		public void KeepConnection()
		{
			this.KNX_CODE.knx_server_is_real(this.LocalEndpoint);
		}

		/// <summary>
		/// 关闭连接
		/// </summary>
		// Token: 0x06000AAE RID: 2734 RVA: 0x000401C0 File Offset: 0x0003E3C0
		public void DisConnectKnx()
		{
			bool flag = this.KNX_CODE.Channel > 0;
			if (flag)
			{
				byte[] array = this.KNX_CODE.Disconnect_knx(this.KNX_CODE.Channel, this.LocalEndpoint);
				this.udpClient.Send(array, array.Length, this.RouEndpoint);
			}
		}

		/// <summary>
		/// 将报文写入KNX系统
		/// </summary>
		/// <param name="addr">地址</param>
		/// <param name="len">长度</param>
		/// <param name="data">数据</param>
		// Token: 0x06000AAF RID: 2735 RVA: 0x00040215 File Offset: 0x0003E415
		public void SetKnxData(short addr, byte len, byte[] data)
		{
			this.KNX_CODE.Knx_Write(addr, len, data);
		}

		/// <summary>
		/// 读取指定KNX组地址
		/// </summary>
		/// <param name="addr">地址</param>
		// Token: 0x06000AB0 RID: 2736 RVA: 0x00040227 File Offset: 0x0003E427
		public void ReadKnxData(short addr)
		{
			this.KNX_CODE.knx_server_is_real(this.LocalEndpoint);
			this.KNX_CODE.Knx_Resd_step1(addr);
		}

		// Token: 0x06000AB1 RID: 2737 RVA: 0x00040249 File Offset: 0x0003E449
		private void KNX_CODE_Set_knx_data(byte[] data)
		{
			this.udpClient.Send(data, data.Length, this.RouEndpoint);
		}

		// Token: 0x06000AB2 RID: 2738 RVA: 0x00040264 File Offset: 0x0003E464
		private void KNX_CODE_GetData_msg(short addr, byte len, byte[] data)
		{
			ILogNet logNet = this.logNet;
			if (logNet != null)
			{
				logNet.WriteDebug(string.Concat(new string[]
				{
					"收到数据 地址：",
					addr.ToString(),
					" 长度:",
					len.ToString(),
					"数据：",
					BitConverter.ToString(data)
				}));
			}
		}

		// Token: 0x06000AB3 RID: 2739 RVA: 0x00040249 File Offset: 0x0003E449
		private void KNX_CODE_Return_data_msg(byte[] data)
		{
			this.udpClient.Send(data, data.Length, this.RouEndpoint);
		}

		// Token: 0x06000AB4 RID: 2740 RVA: 0x000402C4 File Offset: 0x0003E4C4
		private void ReceiveCallback(IAsyncResult iar)
		{
			byte[] array = this.udpClient.EndReceive(iar, ref this._rouEndpoint);
			ILogNet logNet = this.logNet;
			if (logNet != null)
			{
				logNet.WriteDebug("收到报文 {0}", BitConverter.ToString(array));
			}
			this.KNX_CODE.KNX_check(array);
			bool isConnect = this.KNX_CODE.IsConnect;
			if (isConnect)
			{
				this.udpClient.BeginReceive(new AsyncCallback(this.ReceiveCallback), null);
			}
		}

		// Token: 0x04000309 RID: 777
		private const int stateRequestTimerInterval = 60000;

		// Token: 0x0400030A RID: 778
		private IPEndPoint _localEndpoint;

		// Token: 0x0400030B RID: 779
		private IPEndPoint _rouEndpoint;

		// Token: 0x0400030C RID: 780
		private KnxCode KNX_CODE;

		// Token: 0x0400030D RID: 781
		private UdpClient udpClient;

		// Token: 0x0400030E RID: 782
		private ILogNet logNet;
	}
}
